﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace GodSystemCommon
{
    public class Utility
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMd5FromString(string str)
        {
            byte[] bytesData = Encoding.GetEncoding("utf-8").GetBytes(str);
            byte[] hashData = new MD5CryptoServiceProvider().ComputeHash(bytesData);
            StringBuilder result = new StringBuilder(32);
            for (int i = 0; i < hashData.Length; i++)
            {
                result.Append(hashData[i].ToString("x2").ToUpper());
            }
            return result.ToString();
        }

        /// <summary>
        /// 生成目录，绝对路径
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        public static bool CreateDirectory(string directoryPath)
        {
            string subDir = "";
            bool success = true;
            if(directoryPath.IndexOf("\\\\") != -1)
            {
                directoryPath = directoryPath.Replace("\\\\", "\\");
            }
            if(directoryPath.IndexOf("\\") != -1)
            {
                directoryPath = directoryPath.Replace("\\", ",");
            }
            string[] pathRoute = directoryPath.Split(',');
            try { 
                foreach(string path in pathRoute)
                {
                    // 第一次
                    if(subDir == "")
                    {
                        subDir = path;
                        continue;
                    }
                    if(string.IsNullOrEmpty(path))
                    {
                        continue;
                    }
                    subDir += "\\\\" + path;
                    if (Directory.Exists(subDir))
                    {
                        continue;
                    }
                    else
                    {
                        Directory.CreateDirectory(subDir);
                    }
                }
            }catch (Exception)
            {
                success = false;
                throw;
            }
            return success;
        }

        /// <summary>
        /// 根据最终目录和文件名，生成文件
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool creatFile(string directoryPath, string filename)
        {
            bool success = true;
            try
            {
                CreateDirectory(directoryPath);
                if (directoryPath.IndexOf("\\\\") != -1)
                {
                    directoryPath = directoryPath.Replace("\\\\", "\\");
                }
                if (directoryPath.IndexOf("\\") != -1)
                {
                    directoryPath = directoryPath.Replace("\\", ",");
                }
                if (directoryPath.EndsWith(","))
                {
                    directoryPath = directoryPath.Substring(0, directoryPath.Length - 1);
                }
                directoryPath = directoryPath.Replace(",", "\\\\");
                string fullFileName = directoryPath + "\\\\" + filename;
                if (!File.Exists(fullFileName))
                {
                    File.Create(fullFileName);
                }
            }
            catch (Exception)
            {
                success = false;
                throw;
            }
            return success;
        }


        /// <summary>
        /// 内容写文件
        /// </summary>
        /// <param name="fullPathFileName"></param>
        /// <param name="msg"></param>
        public static void WriteFile(string fullPathFileName, string msg)
        {
            try
            {
                DateTime nowTime = DateTime.Now.ToLocalTime();
                msg = "[ " + nowTime.ToString() + " ] " + msg;
                int directoryLength = fullPathFileName.LastIndexOf("\\");

                string directoryName = fullPathFileName.Substring(0, directoryLength);

                string fileName = fullPathFileName.Substring(directoryLength + 1);

                creatFile(directoryName, fileName);
                StreamWriter sw = new StreamWriter(fullPathFileName, true);
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据节点判断是否有对应的权限
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool CheckUserPower(string node)
        {
            bool flag = true;
            if (GlobalData.UsersIsSuperManager == 1)
            {
                return flag;
            }

            if (!GlobalData.userPowers.ContainsValue(node))
            {
                flag = false;
            }

            return flag;
        }
        
    }
}
